package com.example.demofdfs.util.shell;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

/**
 * 结果解析工具
 *
 */
public class ResolveResult {

	/**
	 *<p> 数据格式
	 *<p> 192.168.253.153 | SUCCESS | rc=0 >>
	 *<p>  PID TTY          TIME CMD
	 *<p> 3951 pts/2    00:00:00 sh
	 *<p> 3961 pts/2    00:00:00 python
	 *<p> 3962 pts/2    00:00:00 python
	 *<p> 3963 pts/2    00:00:00 ps
	 */
	public static final String SUCCESS = "| SUCCESS |";
	/**
	 *<p> 192.168.253.153 | FAILED | rc=127 >>
	 *<p>/bin/sh: echo---: command not foundnon-zero return code
	 */
	public static final String FAILED = "| FAILED |";
	/**
	 *<p> 192.168.253.151 | UNREACHABLE! => {
     *<p>"changed": false, 
     *<p>"msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.253.151 port 22: No route to host\r\n", 
     *<p>"unreachable": true
     *<p> }
	 */
	public static final String UNREACHABLE = "| UNREACHABLE!";
	
	public static final String HOST_IP = "ip";
	public static final String HOST_RESULT = "result";
	
	/**
	 * <p> 解析多台主机执行后的结果
	 * <p> 多行数据,返回格式
	 * <p> "ip = 192.168.0.1"
	 * <p> "result = echo hello world"
	 * @param result
	 * @return
	 */
	public static List<Map<String , String>> resolve(String result) {
		// resolves 存放所有主机执行的结果
		List<Map<String , String>> resolves = new ArrayList<>();
		String tmp = result;
		// 把结果按行切割
		String[] lines = tmp.split("\\n");	
		String l;
		List<String> templist = null;
		for (int i = 0; i < lines.length; i++) {
			l = lines[i];
			// 判断是否匹配    SUCCESS / FAILED  / UNREACHABLE 字段
			if (isContains(l)) {
				if (templist == null) {
					templist = new ArrayList<>();
					templist.add(l);
				} else {
					// 获取 一台主机返回的结果
					resolves.add(getResolve(templist));
					templist = new ArrayList<>();
					templist.add(l);
				}
			} else {
				templist.add(l);
			}
			if (i == (lines.length - 1)) {
				resolves.add(getResolve(templist));
			}
		}
		return resolves;
	}
	
	/**
	 * return JSON.toJSONString(object);
	 * @see
	 * @param object
	 * @return
	 */
	public static String toJSONString(Object object) {
		return JSON.toJSONString(object);
	}
	
	/**
	 * JSON.parseObject(text, new TypeReference<List<Map<String, String>>>(){});
	 * @param text
	 * @return
	 */
	public static List<Map<String, String>> parseObject(String text) {
		return JSON.parseObject(text, new TypeReference<List<Map<String, String>>>(){});
	}
	
	private static Map<String , String> getResolve(List<String> list) {
		
		StringBuilder sb = new StringBuilder();
		
		// 获取首行 ip地址  192.168.253.153 | SUCCESS | rc=0 >>
		String firstLine = list.get(0);
		String ip = firstLine.split(" ")[0];
		
		// 判断首行是否 有 大括号 "{", 192.168.253.151 | UNREACHABLE! => { *** }
		if (firstLine.lastIndexOf("{") > -1) 
			sb.append("{");
		
		// 重新拼接命令执行结果
		String l;
		int size;
		String lastLine = list.get((size = list.size()) - 1);
		for (int i = 1; i < size - 1; i++) {
			l = list.get(i);
			sb.append(l).append("\n");
		}
		
		// 切除命令执行失败返回的编码 字样, /bin/sh: echo---: command not foundnon-zero return code
		int index;
		if ((index = lastLine.lastIndexOf("non-zero return code")) > -1)
			sb.append(lastLine.substring(0, index));
		else 
			sb.append(lastLine);
		
		// 返回主机的 ip 和  结果
		Map<String, String> map = new LinkedHashMap<>();
		map.put(HOST_IP, ip);
		map.put(HOST_RESULT, sb.toString());
		return map;
	}
	
	/**
	 * 判断是否匹配    SUCCESS / FAILED  / UNREACHABLE 字段
	 * @param line
	 * @return
	 */
	private static boolean isContains(String line) {
		if (line.contains(SUCCESS))
			return true;
		else if (line.contains(FAILED))
			return true;
		else if (line.contains(UNREACHABLE))
			return true;
		else 
			return false;
	}
	

}
